<template>
  <nav>
    <router-link to="/">首页</router-link>
    <router-link to="/articles">新闻</router-link>
    <template v-if="authStore.user">
      <span>欢迎, {{ authStore.user.username }}</span>
      <button @click="logout">退出</button>
    </template>
    <template v-else>
      <router-link to="/login">登录</router-link>
      <router-link to="/register">注册</router-link>
    </template>
  </nav>
  <router-view />
</template>

<script>
import { useAuthStore } from './stores/auth';
import { useRouter } from 'vue-router';

export default {
  setup() {
    const authStore = useAuthStore();
    const router = useRouter();

    const logout = () => {
      authStore.logout();
      router.push('/');
    };

    return {
      authStore,
      logout
    };
  }
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}

nav {
  padding: 20px;
  background-color: #f8f9fa;
  display: flex;
  gap: 15px;
}

nav a {
  text-decoration: none;
  color: #2c3e50;
}

nav a.router-link-exact-active {
  color: #42b983;
  font-weight: bold;
}

nav button {
  padding: 0 10px;
  background: none;
  border: 1px solid #ddd;
  cursor: pointer;
}
</style>